package cn.gailvlun.gll.presentation.emotion.core

import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.emotion.EmotionSameListViewModel
import cn.gailvlun.gll.presentation.emotion.EmotionSquareViewModel
import cn.gailvlun.gll.presentation.emotion.drawer.EmotionDetail2ViewModel
import cn.gailvlun.gll.presentation.emotion.notification.EmotionNotificationViewModel
import cn.gailvlun.gll.presentation.emotion.drawer.EmotionPersonalViewModel
import cn.gailvlun.gll.presentation.emotion.edit.EmotionRecordViewModel


class ViewModelFactory private constructor(val activity: BaseActivity,
                                           val adapter: BaseAdapter<Any>)
    : ViewModelProvider.NewInstanceFactory() {


    companion object {
        private var INSTANCE: ViewModelFactory? = null
        fun getInstance(activity: BaseActivity, adapter: BaseAdapter<Any>): ViewModelFactory? {
            if (INSTANCE == null) {
                synchronized(ViewModelFactory::class.java) {
                    if (INSTANCE == null) {
                        return ViewModelFactory(activity, adapter)
                    }
                }
            }

            return INSTANCE

        }
    }

    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        when {
            modelClass.isAssignableFrom(EmotionSquareViewModel::class.java) -> return EmotionSquareViewModel(activity, adapter) as T
            modelClass.isAssignableFrom(EmotionPersonalViewModel::class.java) -> {
                return EmotionPersonalViewModel(activity, adapter) as T

            }
            modelClass.isAssignableFrom(EmotionSameListViewModel::class.java) -> {
                return EmotionSameListViewModel(activity, adapter) as T

            }
            modelClass.isAssignableFrom(EmotionRecordViewModel::class.java) -> {


            }
            modelClass.isAssignableFrom(EmotionNotificationViewModel::class.java) -> {
                return EmotionNotificationViewModel(activity, adapter) as T

            }
            modelClass.isAssignableFrom(EmotionDetail2ViewModel::class.java) -> {
                return EmotionDetail2ViewModel(activity, adapter) as T

            }
        }
        throw IllegalArgumentException("unknown ViewModel class:${modelClass.name} ")
    }
}